import { createStore } from 'vuex'
import { Itab } from './type'

interface State {
  tabList: Array<Itab>
}

export const store = createStore<State>({
  state() {
    return {
      tabList: []
    }
  },

  mutations: {
    // 添加tab
    addTab(state: State, tab: Itab) {
      const idx = state.tabList.findIndex(item => item.path === tab.path)
      if (idx === -1) {
        state.tabList.push(tab)
      }
    },

    // 删除tab
    deleteTab(state: State, path) {
      state.tabList = state.tabList.filter(item => item.path !== path)
    }
  },

  getters: {
    getTabList(state: State) {
      return state.tabList
    }
  }
})